﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Drawing.Drawing2D;

namespace rssivisualizer
{
    public partial class frmVisualize : Form
    {
        public frmVisualize()
        {
            InitializeComponent();
        }

        private void btnSimulate_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_A.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_A.txt");

            Plot(act_points, sim_points);
        }

        private void btnSimulateB_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_B.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_B.txt");

            Plot(act_points, sim_points);
        }

        private void btnSimulateC_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_C.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_C.txt");

            Plot(act_points, sim_points);
        }

        private void btnSimAllB_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_BAll.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_BAll.txt");

            Plot(act_points, sim_points);
        }

        private void btnSimulateD_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_D.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_D.txt");

            Plot(act_points, sim_points, true);
        }

        private void btnSim10NodeD_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_D.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_D10.txt");

            Plot(act_points, sim_points, true);
        }

        private void btnAwithC_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_AwithC.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_AwithC.txt");

            Plot(act_points, sim_points);
        }

        private void btnCwithA_Click(object sender, EventArgs e)
        {
            string act_points = File.ReadAllText(@"Data\act_graph_points_CwithA.txt");
            string sim_points = File.ReadAllText(@"Data\sim_graph_points_CwithA.txt");

            Plot(act_points, sim_points);
        }

        private void Plot(string act_points, string sim_points)
        {
            label1.Text = string.Empty;
            pictureBox1.Load(@"Data\Wifi_scenario (Large).PNG");
            string[] act_lines = act_points.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string[] sim_lines = sim_points.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            int total_sim_x = 0;
            int total_sim_y = 0;

            Pen pen_act = new Pen(Color.Blue);
            pen_act.Width = 15.0F;
            Pen pen_sim = new Pen(Color.Red);
            pen_sim.Width = 6.0F;
            Pen pen_avg = new Pen(Color.Black);
            pen_avg.Width = 3.0F;

            const int NUM_SAMPLES = 10;

            for (int i = 0; i < act_lines.Length; i++)
            {
                string act_line = act_lines[i].Trim();
                string[] act_fields = act_line.Split(new char[] { '\t' });

                string sim_line = sim_lines[i].Trim();
                string[] sim_fields = sim_line.Split(new char[] { '\t' });

                int act_x = int.Parse(act_fields[0]);
                int act_y = int.Parse(act_fields[1]);

                int sim_x = int.Parse(sim_fields[0]);
                int sim_y = int.Parse(sim_fields[1]);

                Graphics objGraphic = this.pictureBox1.CreateGraphics();

                objGraphic.DrawEllipse(pen_sim, sim_x, sim_y, 1, 1);
                objGraphic.DrawEllipse(pen_act, act_x, act_y, 1, 1);

                GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);

                total_sim_x += sim_x;
                total_sim_y += sim_y;

                if (i % NUM_SAMPLES == 0)
                {
                    objGraphic = this.pictureBox1.CreateGraphics();

                    int avg_sim_x = total_sim_x/NUM_SAMPLES;
                    int avg_sim_y = total_sim_y/NUM_SAMPLES;

                    objGraphic.DrawLine(pen_avg, avg_sim_x - 7, avg_sim_y - 1, avg_sim_x + 7, avg_sim_y + 1);
                    graph = objGraphic.Save();
                    objGraphic.Restore(graph);

                    total_sim_x = total_sim_y = 0;

                    Thread.Sleep(100);
                    pictureBox1.Refresh();
                }
            }
            label1.Text = "Finished!";
        }


        private void Plot(string act_points, string sim_points, bool D)
        {
            label1.Text = string.Empty;
            pictureBox1.Load(@"Data\Wifi_scenario (Large).PNG");
            string[] act_lines = act_points.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string[] sim_lines = sim_points.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

            int total_sim_x = 0;
            int total_sim_y = 0;

            Pen pen_act = new Pen(Color.Blue);
            pen_act.Width = 15.0F;
            Pen pen_sim = new Pen(Color.Red);
            pen_sim.Width = 6.0F;
            Pen pen_avg = new Pen(Color.Black);
            pen_avg.Width = 3.0F;

            const int NUM_SAMPLES = 10;

            for (int i = 0; i < act_lines.Length; i++)
            {
                string act_line = act_lines[i].Trim();
                string[] act_fields = act_line.Split(new char[] { '\t' });

                string sim_line = sim_lines[i].Trim();
                string[] sim_fields = sim_line.Split(new char[] { '\t' });

                int act_x = int.Parse(act_fields[0]);
                int act_y = int.Parse(act_fields[1]);

                int sim_x = int.Parse(sim_fields[0]);
                int sim_y = int.Parse(sim_fields[1]);

                Graphics objGraphic = this.pictureBox1.CreateGraphics();

                objGraphic.DrawEllipse(pen_sim, sim_x, sim_y, 1, 1);
                objGraphic.DrawEllipse(pen_act, act_x, act_y, 1, 1);

                GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);

                total_sim_x += sim_x;
                total_sim_y += sim_y;

                if (i % NUM_SAMPLES == 0)
                {
                    objGraphic = this.pictureBox1.CreateGraphics();

                    int avg_sim_x = total_sim_x / NUM_SAMPLES;
                    int avg_sim_y = total_sim_y / NUM_SAMPLES;

                    objGraphic.DrawEllipse(pen_avg, avg_sim_x, avg_sim_y, 3,3);
                    graph = objGraphic.Save();
                    objGraphic.Restore(graph);

                    total_sim_x = total_sim_y = 0;

                    Thread.Sleep(100);
                    pictureBox1.Refresh();
                }
            }
            label1.Text = "Finished!";
        }

        private void btnANNDesc_Click(object sender, EventArgs e)
        {
            frmDesc d = new frmDesc();
            d.ShowDialog();
        }










    }
}
